<?php


namespace App\Models\Tool;


use Illuminate\Support\Facades\File;

/**创建添加魔板页面
 * Class MakeAddHtml
 * @package App\Models\Tool
 */
class MakeAddHtmlAndJs
{
    private static  $middjs = [];
    private static  $layuijs = [];
    static function make($html_file,$html_filename,$js_file,$js_filename,$data){
        /*
                [
                    "fied" => "title"
            "type" => "input"
            "title" => "权限节点名称"
            "checktype" => null
          ]*/
        self::$middjs = [];
        self::$layuijs = [
            'layui_use'=>['form'],
            'content'=>[],
            'ajax_back'=>[]
        ];

        $middhtml = [];

        foreach ($data as $data1){
            switch($data1['type']) {

                case 'input':
                    $formdata= self::form_input($data1);
                    if($formdata['html'])$middhtml[] = $formdata['html'];
                    if($formdata['js'])self::$middjs[] = $formdata['js'];
                    break;
                case 'textarea':
                    $formdata= self::form_textarea($data1);
                    if($formdata['html'])$middhtml[] = $formdata['html'];
                    if($formdata['js'])self::$middjs[] = $formdata['js'];
                    break;

                case 'select':
                    $formdata= self::form_select($data1);
                    if($formdata['html'])$middhtml[] = $formdata['html'];
                    if($formdata['js'])self::$middjs[] = $formdata['js'];
                    break;
                case 'radio':
                    $formdata= self::form_radio($data1);
                    if($formdata['html'])$middhtml[] = $formdata['html'];
                    if($formdata['js'])self::$middjs[] = $formdata['js'];
                    break;
                case 'checkbox':
                    $formdata= self::form_checkbox($data1);
                    if($formdata['html'])$middhtml[] = $formdata['html'];
                    if($formdata['js'])self::$middjs[] = $formdata['js'];
                    break;
                case 'uploadimage':
                    $formdata= self::form_uploadimage($data1);
                    if($formdata['html'])$middhtml[] = $formdata['html'];
                    if($formdata['js'])self::$middjs[] = $formdata['js'];
                    break;
                case 'uploadfile':
                    $formdata= self::form_uploadfile($data1);
                    if($formdata['html'])$middhtml[] = $formdata['html'];
                    if($formdata['js'])self::$middjs[] = $formdata['js'];
                    break;
                case 'layedit':
                    $formdata= self::form_layedit($data1);
                    if($formdata['html'])$middhtml[] = $formdata['html'];
                    if($formdata['js'])self::$middjs[] = $formdata['js'];
                    break;
                default:

            }

        }

        self::make_html($html_file,$html_filename,$js_file,$middhtml);
        self::make_js($js_file,$js_filename,self::$middjs);

    }


    /**生成html
     * @param $html_file
     * @param $html_filename
     * @param $js_file
     * @param $middhtml
     */
    private static function make_html($html_file,$html_filename,$js_file,$middhtml){
        $middhtml = join("\n", $middhtml);
        $js_file=str_replace('\\','/',$js_file).'?'.date('Y_m_d_H_i_s');
        $makecontent=<<<text
@extends('admin.defaults.head')
@section('title','标题')
@section('body')

{{--请在下面编辑body 内容--}}
<div class="layui-form layui-form-pane" lay-filter="layuiadmin-form-role" id="layuiadmin-form-role" style="padding: 20px;background: #fff;">
<input type="hidden" name="id">
{$middhtml}

    <div class="layui-form-item layui-hide">
        <button class="layui-btn" lay-submit lay-filter="LAY-user-role-submit" id="LAY-user-role-submit">提交</button>
    </div>
</div>

{{--上方body内容结束   请在下面编辑script 内容--}}

@endsection
@section('script')
<script src="/{$js_file}"></script>
@endsection
text;
        $html_file=resource_path($html_file);
        // 写入文件，存在的话覆盖写入
        File::replace(($html_file), $makecontent);
    }

    /**生成js
     * @param $js_file
     * @param $js_filename
     * @param $middjs
     */
    private static function make_js($js_file,$js_filename,$middjs){
        $middhtml = join("\n", $middjs);
        $js_file=public_path($js_file);

        $layui_use='"'.join('","',self::$layuijs['layui_use']).'"';
        $layui_content=join("\n", self::$layuijs['content']);;
        $layui_ajax_back=join("\n", self::$layuijs['ajax_back']);;


        $makecontent=<<<text
{$middhtml}

var $, form;
layui.use([{$layui_use}], function () {
    $ = layui.$
        , form = layui.form;
    var id = getPar('id');
    if(id>0){
    layer.load(1)

        $.ajax({
            url: './info',
            type: 'GET',
            data: {id: id},
            success: function (result) {
                if (result.code == 200) {
                    //layuiadmin-form-role 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    //给表单赋值
                    form.val("layuiadmin-form-role",result.data);
                    {$layui_ajax_back}
                } else {
                    layer.msg(result.message)
                }
                layer.closeAll();
            }
        });
    }

    {$layui_content}
})
text;

        // 写入文件，存在的话覆盖写入
        File::replace($js_file, $makecontent);
    }

//单行输入框
    private static function form_input($data1){
        $html=<<<text
<div class="layui-form-item">
    <label class="layui-form-label">{$data1['title']}</label>
    <div class="layui-input-block">
      <input type="text" name="{$data1['fied']}"   lay-verify="{$data1['checktype']}" placeholder="请输入{$data1['title']}" autocomplete="off" class="layui-input">
    </div>
  </div>
text;
        $js = '';
        return ['html'=>$html,'js'=>$js];
    }
//多行输入框
    private static function form_textarea($data1){
        $html=<<<text
 <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">{$data1['title']}</label>
    <div class="layui-input-block">
      <textarea name="{$data1['fied']}"   lay-verify="{$data1['checktype']}" placeholder="请输入{$data1['title']}" class="layui-textarea"></textarea>
    </div>
  </div>
text;
        $js = '';
        return ['html'=>$html,'js'=>$js];
    }

    //下拉选择框
    private static function form_select($data1){
        $html=<<<text
<div class="layui-form-item">
      <label class="layui-form-label">{$data1['title']}</label>
      <div class="layui-input-block">
        <select name="{$data1['fied']}">
          <option value="">请选择</option>

        </select>
      </div>
  </div>
text;
        $js = '';
        return ['html'=>$html,'js'=>$js];
    }

    //单选框
    private static function form_radio($data1){
        $html=<<<text
<div class="layui-form-item">
    <label class="layui-form-label">{$data1['title']}</label>
    <div class="layui-input-block">
      <input type="radio" name="{$data1['fied']}" value="1" title="自行修改1" >
      <input type="radio" name="{$data1['fied']}" value="0" title="自行修改0" checked="">

    </div>
  </div>
text;
        $js = '';
        return ['html'=>$html,'js'=>$js];
    }
    //多选框
    private static function form_checkbox($data1){
        $html=<<<text
  <div class="layui-form-item" pane="">
    <label class="layui-form-label">{$data1['title']}</label>
    <div class="layui-input-block">
      <input type="checkbox" name="{$data1['fied']}[]" lay-skin="primary" value="1" title="自行修改1" checked="">
      <input type="checkbox" name="{$data1['fied']}[]" lay-skin="primary" value="0" title="自行修改0">

    </div>
  </div>
text;

        $js = '';
        return ['html'=>$html,'js'=>$js];
    }
    //图片上传
    private static function form_uploadimage($data1){
        $html=<<<text

<div class="layui-form-item">
                        <label class="layui-form-label">{$data1['title']}</label>
                        <div class="layui-input-block" style="text-align: center">
                            <img  id="{$data1['fied']}_show_img" style="width: 100%;max-height: 222px;margin-bottom: 10px" alt="">
                            <input type="hidden" name="{$data1['fied']}"  id="{$data1['fied']}">
                            <button type="button" class="layui-btn" id="{$data1['fied']}_upload"><i class="layui-icon"></i>上传图片</button>

                            <button class="layui-btn layui-btn-danger "  onclick="deleimg()">
                                <i class="layui-icon layui-icon-delete"></i>删除图片</button>

                        </div>
                    </div>
text;

        if(!in_array('upload',self::$layuijs['layui_use'])){
            self::$layuijs['layui_use'][] = 'upload';
            self::$layuijs['content'][] = 'var  upload = layui.upload;';
        }
        self::$layuijs['content'][]= <<<text

//指定允许上传的文件类型
        upload.render({
            elem: '#{$data1['fied']}_upload'
            ,url: '/admin/upload/img'
            ,accept: 'file' //普通文件
            ,done: function(res){
                $("#{$data1['fied']}_show_img").attr('src',res.data+"?_="+Math.random());
                $("#{$data1['fied']}").val(res.data);
            }
        });

text;
        $js = '';
        return ['html'=>$html,'js'=>$js];
    }
    //文件上传
    private static function form_uploadfile($data1){
        $html=<<<text
 <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">{$data1['title']}</label>
    <div class="layui-input-block">
      <textarea name="{$data1['fied']}"   lay-verify="{$data1['checktype']}" placeholder="请输入{$data1['title']}" class="layui-textarea"></textarea>
    </div>
  </div>
text;

        $js = '';
        return ['html'=>$html,'js'=>$js];
    }
    //layui富文本编辑器
    private static function form_layedit($data1){
        $html=<<<text
<div class="layui-form-item layui-form-text">
          <label class="layui-form-label">{$data1['title']}</label>
          <div class="layui-input-block">
            <textarea name="{$data1['fied']}"  id="{$data1['fied']}"  lay-verify="{$data1['checktype']}" placeholder="请输入{$data1['title']}"  class="layui-textarea"></textarea>
          </div>
        </div>
text;

        $js =<<<text
//layui富文本编辑器++++
var tinymcelayui;
layui.extend({
        tinymce: '/style_js_admin/layuiadmin/layui_exts/tinymce/tinymce'
    }).use(['tinymce'], function(){
      tinymcelayui = layui.tinymce
      ;
    var edit = tinymcelayui.render({
            elem: "#{$data1['fied']}"
            , height: 300
            ,images_upload_url: '/admin/upload/img'
            ,images_upload_base_path: '/demo'
            // 其余配置可参考官方文档
        },function(opt,edit){
            //加载完成后回调
        });

        $("#LAY-user-role-submit").on('click',function(e){
            $("#{$data1['fied']}").val(tinymcelayui.get('#{$data1['fied']}').getContent())
        })

});
//layui富文本编辑器---
text;
        ;
        self::$layuijs['ajax_back'][]=<<<text


 tinymcelayui.get('#{$data1['fied']}').setContent(result.data.{$data1['fied']})


text;

        return ['html'=>$html,'js'=>$js];
    }





}
